package com.tool.finances.lifestyle;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.tool.finances.lifestyle.data.AccountProvider;
import com.tool.finances.lifestyle.data.AccountType;

public class CategorySetting extends Activity {

	private ArrayAdapter<String> adapter;
	private Spinner categorySpinner;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setTheme(android.R.style.Theme_Dialog);
		super.onCreate(savedInstanceState);
		setTitle("Category setting");
		setContentView(R.layout.category_setting);
		Button addCategoryButton = (Button) findViewById(R.id.AddCategoryButton);
		Button deleteCategoryButton = (Button) findViewById(R.id.DeleteCategoryButton);
		final EditText addCategoryText = (EditText) findViewById(R.id.AddCategoryText);
		categorySpinner = (Spinner) findViewById(R.id.CategoryEditSpinner);

		addCategoryButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!addCategoryText.getText().toString().equalsIgnoreCase("")) {
					ContentValues values = new ContentValues();
					values.put(AccountType.Account.CATEGORYTYPE,
							addCategoryText.getText().toString());
					Uri uri = null;
					try {
						uri = getContentResolver().insert(
								AccountProvider.CATEGORY_CONTENT_URI, values);
					} catch (Exception e) {
						Toast.makeText(CategorySetting.this, "Add failed.",
								1000).show();
					}

					if (null != uri) {
						Toast.makeText(CategorySetting.this,
								"Add successfully", 1000).show();
						adapter.notifyDataSetChanged();
					}
					addCategoryText.setText("");
				} else {
					Toast.makeText(CategorySetting.this,
							"Category can't be empty.", 1000).show();
				}
				onResume();
			}
		});

		deleteCategoryButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String c = categorySpinner.getSelectedItem().toString();
				if (1 == categorySpinner.getCount()) {
					Toast.makeText(CategorySetting.this,
							"The category can't be deleted.",
							Toast.LENGTH_SHORT).show();
				} else {
					if (!c.equalsIgnoreCase("")
							&& categorySpinner.getCount() > -1) {
						int row = getContentResolver().delete(
								AccountProvider.CATEGORY_CONTENT_URI,
								"category = '" + c + "' ", null);
						if (row < 1) {
							Toast.makeText(CategorySetting.this,
									"Delete failed.", Toast.LENGTH_SHORT)
									.show();
						}
						Toast.makeText(CategorySetting.this,
								"Delete successfully", Toast.LENGTH_SHORT);
						addCategoryText.setText("");
						onResume();
					}
				}
			}
		});
	}

	@Override
	protected void onResume() {
		Cursor cursor = getContentResolver().query(
				AccountProvider.CATEGORY_CONTENT_URI,
				AccountType.Account.CATEGORYPROECTION, null, null, null);
		String[] group = getStringGroup(cursor);
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, group);
		categorySpinner.setAdapter(adapter);
		super.onResume();
	}

	private String[] getStringGroup(Cursor cursor) {
		String[] group = new String[cursor.getCount()];
		int i = 0;
		if (cursor.moveToFirst()) {
			do {
				int index = cursor
						.getColumnIndex(AccountType.Account.CATEGORYTYPE);
				group[i] = cursor.getString(index);
				i++;
			} while (cursor.moveToNext());
		}
		return group;
	}
}
